package com.mmp.main.servlet.device;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.bo.Message;
import com.mmp.common.servlet.BasicServlet;
import com.mmp.common.util.Common;
import com.mmp.common.util.FileUtil;
import com.mmp.main.entity.HospitalDevice;
import com.mmp.main.entity.HospitalDeviceTransferringInfo;
import com.mmp.main.util.Default;
import com.mmp.main.util.Enums;
import com.mmp.main.util.WebUtil;

@MultipartConfig
public class DeviceTransferringProcessServlet extends BasicServlet {

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {

		int deviceId = WebUtil.getParamValueAsInteger(request, "deviceId", 0);

		if (deviceId == 0) {
			announceErorrMessage(request, response, Default.GENERAL_ERROR_MESSAGE);
			return;
		}

		HospitalDeviceTransferringInfo transferInfo = new HospitalDeviceTransferringInfo();

		HospitalDevice device = dbQueryManager.findById(deviceId, HospitalDevice.class);
		device.setStatus(Enums.DEVICE_STATUS.TRANSFERRED.getStatus());
		transferInfo.setHospitalDevice(device);

		transferInfo.setTransferDate(WebUtil.getParamValueAsDate(request, "transferDate", null));

		transferInfo.setTransferReason(WebUtil.getParamValue(request, "transferReason", null));

		transferInfo.setTransferDestination(WebUtil.getParamValue(request, "transferDestination", null));

		String[] transferReportUrl = FileUtil.uploadFiles(request, "transferReportUrl");
		// if (transferReportUrl != null && transferReportUrl.length != 0)
		transferInfo.setTransferReportUrl(Common.concatenateValues(transferReportUrl));

		transferInfo.setCreatedBy(getSessionUser(request));

		transferInfo.setCreatedIn(new Date());

		dbQueryManager.mergeEntity(transferInfo);

		setMessage(request, new Message("تم تسجيل بيانات النقل  أو السحب بنجاح", Message.MESSAGE_TYPE.SUCCESS));

		sendRedirect(request, response, "TransferDeviceSearch");

	}

}
